var config = require('../../config.js')
var http = require('../../utils/httpHelper.js')
Page({
  data: {
    userInfo:{},
    isLogin:false,
    indicatorDots:true,//是否显示面板指示点
    autoplay:true, //是否自动切换
    interval:5000,//自动切换时间
    duration:1000, //滑动时间
    buyCount:1,
    goods:{}
  },
  onLoad:function(options){
    var that = this;
    let userInfo = wx.getStorageSync('user');
    if (userInfo!=null&&userInfo!=undefined&&userInfo!={}&&userInfo!='') {
      wx.showShareMenu({
        withShareTicket:true,
        menus:['shareAppMessage','shareTimeline']
      })
      that.setData({
        isLogin:true,
        userInfo:userInfo
      })
    }else {
      wx.hideShareMenu();
      wx.setStorageSync('inviteId', options.inviteId);
    }
    var id = options.id; 
    that.getGoodsInfo(id);
  },
  onShow:function(){
    let userInfo = wx.getStorageSync('user');
    if (userInfo!=null&&userInfo!=undefined&&userInfo!={}&&userInfo!='') {
      this.setData({
        isLogin:true,
        userInfo:userInfo
      })
    }
    if (!this.data.isLogin) {
      wx.hideShareMenu()
    }
  },
  getGoodsInfo:function(id){
    var curUserId = 0;
    if (this.data.userInfo.userId) {
      curUserId = this.data.userInfo.userId;
    }
    var that = this;
    var data = {id:id,userId:curUserId}
    http.httpGet(config.clubApi.getProductInfo,data,function(res){
      if(res.code == '200'){
        var goods = res.data;
        that.setData({goods:goods});
      }
    });
  },
  buyCount:function(e){
    var id = e.currentTarget.id;
    var count = this.data.buyCount;
    if(id == "add"){
      count = (count>0)?count+1:1
    }else {
      count = (count>0)?count-1:0
    }
    this.setData({buyCount:count});
    this.buyNow('');
  },
  buyNow:function(e){
    if (this.data.isLogin&&wx.getStorageSync('user')) {
      var count = this.data.buyCount;
      count = (count>0)?count:1;
      var goods = this.data.goods;
      //取出订单商品
      goods = {id:goods.id,shopId:goods.shopId,name:goods.productTitle,img:goods.productPic,price:goods.productPrice,discount:goods.productDiscount,buycount:count};
      try{
        var allGoods = wx.getStorageSync('shoppingcar')
        if(allGoods == ""){
          allGoods = []
        }
        //检查订单是否有此商品
        var hasCount = 0;
        for(var i=0;i<allGoods.length;i++){
          var temp = allGoods[i];
          if(temp.id == goods.id){
            hasCount = temp.buycount;
            allGoods.splice(i, 1);
            break;
          }
        }
        goods.buycount = goods.buycount + hasCount;
        allGoods.push(goods);
        wx.setStorageSync('shoppingcar', allGoods);
      }catch(m){
        console.log('立即购买失败!');
      }
      if(e != ''){
        var currentPagess = getCurrentPages();
        wx.navigateBack({
          delta: 1, // 回退前 delta(默认为1) 页面
          success: function(res){
            wx.navigateTo({ url: '/pages/shoppingcar/index' })
          }
        })
      }
    }else {
      wx.switchTab({ url: '/pages/user/index' });
    }
  },
  // 分享到朋友圈
  onShareTimeline() {
    var goods = this.data.goods;
    var url = '/pages/detail/index?id='+goods.id+'&inviteId='+this.data.userInfo.userId;
    return {
      title: goods.productTitle,
      path: url,
      imageUrl: goods.productPic,
      desc: goods.productDesc
    }
  },
  // 转发
  onShareAppMessage() {
    var goods = this.data.goods;
    var url = '/pages/detail/index?id='+goods.id+'&inviteId='+this.data.userInfo.userId;
    return {
      title: goods.productTitle,
      path: url,
      imageUrl: goods.productPic,
      desc: goods.productDesc
    }
  }
})